// pages/mine/mine.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUseNicknameComp: wx.canIUse('button.open-type.chooseAvatar'),
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    showCenter: false
  },

  // 获取用户信息
  getUserInfo() {
    const token = wx.getStorageSync('token');
    const userInfoStorage = wx.getStorageSync('userInfo');
    console.log('本地userInfo:', userInfoStorage);
    if (!token || !userInfoStorage || !userInfoStorage.openid) {
      this.setData({
        hasUserInfo: false,
        userInfo: {}
      });
      return;
    }

    wx.request({
      url: 'http://localhost:8080/users/wx/' + userInfoStorage.openid,
      method: 'GET',
      header: {
        'Authorization': token
      },
      success: (res) => {
        console.log('获取用户信息返回：', res);
        console.log('接口返回data.data:', res.data.data);
        if (res.data.success) {
          const userData = res.data.data;
          let avatarUrl = '';
          if (userData.avatar) {
            if (userData.avatar.startsWith('/images/')) {
              avatarUrl = userData.avatar;
            } else if (userData.avatar.startsWith('http')) {
              avatarUrl = userData.avatar;
            } else {
              avatarUrl = 'http://localhost:8080' + userData.avatar.replace(/^\./, '');
            }
          }
          this.setData({
            hasUserInfo: true,
            userInfo: {
              avatarUrl: avatarUrl,
              nickName: userData.nickname || '未设置昵称',
              phone: userData.phone || '未绑定手机号'
            }
          });
          console.log('setData后userInfo:', this.data.userInfo);
          app.globalData.userInfo = this.data.userInfo;
        } else {
          this.setData({
            hasUserInfo: false,
            userInfo: {}
          });
        }
      },
      fail: (error) => {
        wx.showToast({
          title: '获取用户信息失败',
          icon: 'none'
        });
      }
    });
  },

  onLoad() {
    this.getUserInfo();
  },

  onShow() {
    this.getUserInfo();
  },

  onChooseAvatar(e) {
    this.setData({
      'userInfo.avatarUrl': e.detail.avatarUrl
    });
  },
  onInputChange(e) {
    this.setData({
      'userInfo.nickName': e.detail.value
    });
  },
  
  goToCenter() {
    wx.navigateTo({
      url: '/pages/center/center'
    });
  },
  backFromCenter() {
    this.setData({
      showCenter: false
    });
  },
  goToMyCourse() {
    wx.navigateTo({
      url: '/pages/mine/mycourse/mycourse'
    });
  },

  goToMyFavorite() {
    wx.navigateTo({
      url: '/pages/mine/myfavorite/myfavorite'
    });
  },

  goToMyOrder() {
    wx.navigateTo({
      url: '/pages/mine/myorder/myorder'
    });
  },

  goToQuestion() {
    wx.navigateTo({
      url: '/pages/mine/question/question'
    });
  },

  goToSuggest() {
    wx.navigateTo({
      url: '/pages/mine/suggest/suggest'
    });
  },

  goToSystem() {
    wx.navigateTo({
      url: '/pages/mine/system/system'
    });
  },
})